package cn.melon.view;

import java.awt.Color;
import java.awt.Cursor;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Insets;
import java.awt.RenderingHints;
import java.awt.image.BufferedImage;

import javax.swing.JToggleButton;

import cn.melon.helper.BufferedImageHelper;

/**
 * @ClassName: JImgButton
 * @author Hzy
 * 继承JButton类，各类信息页面左部的选择控件，可设置背景图片
 */
public class JImgButton extends JToggleButton{
	private final int ICON_SIZE = 33;
	private String originImgPath;
	private String changedImgPath;
	private String buttonName;
	private int width;
	private int height;
	private Font font;
	private int gap;
	private Color originColor;
	private Color changedColor;
	private Color originBacColor;
	private Color changedBacColor;
	
	private boolean changeWithoutSelected;//不需要设置selected即重绘
	
	public JImgButton(int width,int height,String originImgPath,String changedImgPath,String buttonName,Font font,int gap,Color originColor,Color changedColor,Color originBacColor,Color changedBacColor){
		super();
		this.setBorder(null);
		this.setBorderPainted(false);
		this.setMargin(new Insets(0,0,0,0));                 
		this.setFocusPainted(false);
		this.setPreferredSize(new Dimension(width,height));
		this.width = width;//button宽度
		this.height= height;//button高度
		this.originImgPath = originImgPath;//icon路径
		this.changedImgPath = changedImgPath;
		this.buttonName = buttonName;//按钮名称
		this.font = font;//字体样式
		this.gap = gap;//icon和文字之间的间隔
		this.originColor = originColor;//文字颜色
		this.changedColor = changedColor;
		this.originBacColor = originBacColor;//背景颜色
		this.changedBacColor = changedBacColor;//背景颜色
		this.changeWithoutSelected = false;
		
		this.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
	}
	@Override
	public void paintComponent(Graphics g){
//		super.paintComponent(g);
		Graphics2D g2d = (Graphics2D) g;
		Color bacColor = originBacColor;
		Color color = originColor;
		String imgPath = originImgPath;
		if(this.isSelected() || changeWithoutSelected){
			bacColor = changedBacColor;
			color = changedColor;
			imgPath = changedImgPath;
		}
//		先清理之前的图
		g2d.clearRect(0, 0, width, height);
		g2d.setColor(bacColor);
		g2d.fillRect(0, 0, width, height);
//		获取图片
		BufferedImage icon = new BufferedImage(width,height,BufferedImage.TYPE_4BYTE_ABGR);
		icon = BufferedImageHelper.getSource(this.getClass(), imgPath);
//		try{
//			icon = ImageIO.read(new File(imgPath));
//		}catch(Exception e){
//			e.printStackTrace();
//		}
//		计算Icon和text的绘制位置
		FontMetrics metrics = g2d.getFontMetrics(font);
		int fontWidth = metrics.stringWidth(buttonName);
		int fontHeight = metrics.getHeight();
		int x_icon = (width - ICON_SIZE - fontWidth -gap)/2;
		int y_icon = (height - ICON_SIZE)/2;
		int x_text = x_icon + ICON_SIZE + gap;
		int y_text = (height - fontHeight)/2 + metrics.getAscent();
//		g2d设置
		g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);//开启抗锯齿
		g2d.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);//渲染质量高
		g2d.setRenderingHint(RenderingHints.KEY_DITHERING, RenderingHints.VALUE_DITHER_DISABLE);//关闭抖动
		g2d.setRenderingHint(RenderingHints.KEY_STROKE_CONTROL, RenderingHints.VALUE_STROKE_PURE);//画笔控制
//		绘制Icon和text
		g2d.setFont(font);
		g2d.setColor(color);
		g2d.drawImage(icon, x_icon, y_icon, ICON_SIZE, ICON_SIZE, null);
		g2d.drawString(buttonName, x_text, y_text);
	}
	public void repaintChangedButton(){
		this.changeWithoutSelected = true;
		this.repaint();
	} 
	public void repaintOriginButton(){
		this.changeWithoutSelected = false;
		this.repaint();		
	}
}
